﻿using Authorization.Entitys;
using Authorization.Entitys.Model;
using Authorization.Entitys.Models;
using Authorization.Entitys.VM;
using Authorization.Infrastructure.Extensions;
using Authorization.IRepository;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Naruto.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Authorization.Repository
{
    /// <summary>
    /// 用户操作仓储
    /// </summary>
    public class UserRepository : IUserRepository
    {

        private readonly IRepository<AspNetIdentityDbContext> repository;

        private readonly UserManager<AuthorizationUser> userManager;

        public UserRepository(IRepository<AspNetIdentityDbContext> _repository, UserManager<AuthorizationUser> _userManager)
        {
            repository = _repository;
            userManager = _userManager;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="pswd"></param>
        /// <returns></returns>
        public async Task UpdatePasswordByUserId(string userId, string pswd)
        {
            await repository.Command<User>()
                   .UpdateAsync(a => a.Id == userId,
                   entity =>
                   {
                       entity.Password = pswd;
                       return entity;
                   });
            await repository.Command<AuthorizationUser>().UpdateAsync(a => a.Id == userId, entity =>
             {
                 entity.PasswordHash = userManager.GetPassword(pswd);
                 return entity;
             });
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="pswd"></param>
        /// <returns></returns>
        public async Task UpdatePasswordByEmail(string email, string pswd)
        {
            await repository.Command<User>()
                   .UpdateAsync(a => a.Email == email,
                   entity =>
                   {
                       entity.Password = pswd;
                       return entity;
                   });
            await repository.Command<AuthorizationUser>().UpdateAsync(a => a.Email == email, entity =>
            {
                entity.PasswordHash = userManager.GetPassword(pswd);
                return entity;
            });
        }

        /// <summary>
        /// 验证用户名是否存在
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task<bool> ExistsUserName(string userName)
        {
            var userQueryable = await repository.QueryAsync<User>();
            return await userQueryable.Where(a => a.UserName == userName).AsNoTracking().AnyAsync();
        }
        /// <summary>
        /// 验证邮件是否存在
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public async Task<bool> ExistsEmail(string email)
        {
            var userQueryable = await repository.QueryAsync<User>();
            return await userQueryable.Where(a => a.Email == email).AsNoTracking().AnyAsync();
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public async Task Add(User info)
        {
            await repository.Command<User>().AddAsync(info);
            await repository.Command<AuthorizationUser>().AddAsync(new AuthorizationUser
            {
                Id = info.Id,
                Email = info.Email,
                NormalizedEmail = info.Email.ToUpper(),
                UserName = info.UserName,
                NormalizedUserName = info.UserName.ToUpper(),
                PasswordHash = userManager.GetPassword(info.Password),
            });
        }
    }
}
